package com.fingard.dsp.bank.directbank.cmb02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IContract;
import com.fingard.dsp.bank.directbank.cmb02.util.SignUtil;
import com.fingard.dsp.bank.reqrespobj.contract.*;
import com.fingard.dsp.bank.reqrespobj.contract.detail.RetAtsContractDetail;
import com.fingard.io.FileHelper;

import java.io.File;
import java.util.Date;

public class CMB02Contract extends CMB02Base implements IContract {
    @Override
    public RetAtsContract contract(ReqContract p_req) throws Exception {
        return null;
    }

    @Override
    public RetAtsContract upContract(ReqContract p_req) throws Exception {
        return null;
    }

    @Override
    public RetAtsContract qryContract(ReqContract p_reqQry) throws Exception {
        RetAtsContract retAts = new RetAtsContract(p_reqQry.reqHeader);
        RetAtsContractDetail tmpRetDetail = new RetAtsContractDetail();
        tmpRetDetail.reqSeqID = p_reqQry.contractDetail.askNumber;

        try {
            String tmpMacKey = "";
            String tmpMacKeyPath = getCfgDirectory() + File.separator + "macKey";
            if (!new File(tmpMacKeyPath).exists()) {
                String[] tmpMacKeyRet = exchangeMacKey();
                if (tmpMacKeyRet[0].length() != 0) {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpMacKeyRet[1];
                } else {
                    tmpMacKey = FileHelper.readFileText(tmpMacKeyPath, getCharset());
                }
            } else {
                tmpMacKey = FileHelper.readFileText(tmpMacKeyPath, getCharset());
            }
            WriteBankLogLn("macKey:" + tmpMacKey);

            StringBuilder tmpEachRecordReq = new StringBuilder();

            StringBuilder tmpEachRecordBody = new StringBuilder();
            tmpEachRecordBody.append(String.format("%-20s", bankFront.corpNo));
            tmpEachRecordBody.append(String.format("%-20s", bankFront.org));
            tmpEachRecordBody.append("3");
            tmpEachRecordBody.append(String.format("%-25s", ""));
            tmpEachRecordBody.append(String.format("%-20s", ""));
            tmpEachRecordBody.append(String.format("%-20s", p_reqQry.contractDetail.payAct));
            tmpEachRecordBody.append(String.format("%-32s", ""));
            tmpEachRecordBody.append(String.format("%-50s", ""));

            StringBuilder tmpEachRecordHeader = new StringBuilder();
            tmpEachRecordHeader.append("CMBA");
            tmpEachRecordHeader.append(String.format("%-4s", tmpEachRecordBody.toString().getBytes(getCharset()).length));
            tmpEachRecordHeader.append("CMJX");
            tmpEachRecordHeader.append(String.format("%-8s", ""));
            tmpEachRecordHeader.append(String.format("%-6s", ""));
            tmpEachRecordHeader.append(String.format("%-30s", ""));

            Date tmpDate = new Date();
            tmpEachRecordHeader.append(Format.Date8Format.format(tmpDate));
            tmpEachRecordHeader.append(Format.Time6Format.format(tmpDate));
            tmpEachRecordHeader.append(String.format("%-25s", getQryTransID()));
            tmpEachRecordHeader.append("0.2.0");
            tmpEachRecordHeader.append(String.format("%-10s", bankFront.corpNo));
            tmpEachRecordHeader.append(String.format("%-16s", SignUtil.ansi99(tmpMacKey, tmpEachRecordBody.toString())));

            tmpEachRecordReq.append(tmpEachRecordHeader).append(tmpEachRecordBody);

            String[] tmpStrRet = sendToBank(tmpEachRecordReq.toString());
            if (tmpStrRet[0].length() == 0) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = tmpStrRet[1].substring(52).trim();
                String tmpRespCode = tmpStrRet[1].substring(0, 7);
                if ("CMBMB99".equals(tmpRespCode)) {
                    String authStatus = tmpStrRet[1].substring(7,8);
                    if ("A".equalsIgnoreCase(authStatus)) {
                        tmpRetDetail.signState = ContractState.SUCCESS;
                        tmpRetDetail.authCode = tmpStrRet[1].substring(8,28);
                        tmpRetDetail.payAct = p_reqQry.contractDetail.payAct;
                    } else if ("D".equalsIgnoreCase(authStatus) || "Z".equalsIgnoreCase(authStatus)) {
                        tmpRetDetail.signState = ContractState.UNSIGNED;
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpRespCode + "+" + tmpStrRet[1].substring(52).trim();
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsQryBatContract qryBatContract(ReqQryBatContract p_reqQry) throws Exception {
        return null;
    }
}